﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Web.Hosting;
using System.ServiceModel.Activation;

namespace jacService
{
    
    public class Service1 : IService1
    {
        adDataDataContext dc = new adDataDataContext();

        string filedir = HostingEnvironment.ApplicationPhysicalPath + @"data\";

        #region IService1 成员

        public byte[] getByte(string fileName)
        {
            return File.ReadAllBytes(filedir + fileName);
        }

        public List<ad> getAD()
        {
            try
            {
                return dc.GetTable<ad>().ToList();
            }
            catch
            {
                return new List<ad>().ToList();
            }

        }

        public bool addAD(ad data, bytedata ms)
        {
            add(data, ms);
            return true;
        }

        public ad selectAD(int id)
        {
            return dc.ad.Single(d => d.id == id);
        }

        public bool delete(int id)
        {
            if ((from d in dc.ad where d.id == id select d).Count() > 0)
            {
                var data = dc.ad.Single(d => d.id == id);

                //删除文件
                if (File.Exists(filedir + data.video))
                {
                    File.Delete(filedir + data.video);
                }
                if (File.Exists(filedir + data.img))
                {
                    File.Delete(filedir + data.img);
                }

                //删除数据
                dc.ad.DeleteOnSubmit(data);
                dc.SubmitChanges();
            }
            return true;
        }

        public bool insdata()
        {
            try
            {
                var myad = dc.GetTable<ad>();
                foreach (var mad in myad)
                {
                    dc.ad.DeleteOnSubmit(mad);
                }
                dc.SubmitChanges();
                Directory.Delete(HostingEnvironment.ApplicationPhysicalPath + "data",true);
                Directory.CreateDirectory(HostingEnvironment.ApplicationPhysicalPath + "data");
                return true;
            }
            catch
            {
                return false;
            }
        }

        public List<setting> getsetting()
        {
            return dc.GetTable<setting>().ToList();
        }

        public bool setdata(string key, string data)
        {
            var ndata = dc.setting.Single(d => d.set_name == key);
            ndata.set_value = data;
            dc.SubmitChanges();
            return true;
        }

        public bool login(string userid, string pwd)
        {
            if ((from i in dc.admin where i.admin_name==userid select i).Count() >0)
            {
                admin data = dc.admin.Single(d => d.admin_name == userid);

                if (data.admin_pwd == pwd)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }

        public bool updatepwd(string oldpwd, string pwd)
        {
            var user = dc.admin.Single(d => d.admin_name == "admin");
            if (oldpwd.Equals(user.admin_pwd))
            {
                user.admin_pwd = pwd;
                dc.SubmitChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        void add(ad data, bytedata ms)
        {
            if (data.sort == 0)
            {
                StreamToFile(ms.filedata, filedir + data.img);
                dc.ad.InsertOnSubmit(data);
                dc.SubmitChanges();
            }
            else if (data.sort == 1)
            {
                StreamToFile(ms.filedata, filedir + data.video);
                dc.ad.InsertOnSubmit(data);
                dc.SubmitChanges();
            }
        }


        void StreamToFile(byte[] bytes, string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }
    }

    [DataContract]
    public class bytedata
    {
        [DataMember]
        public byte[] filedata { get; set; }
    }

}
